package ac.qnlm.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    public static List<String> getFiles(String path) {
        List<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile() && (tempList[i].toString().endsWith(".cu"))) {
                files.add(tempList[i].toString());
            }
            if (tempList[i].isDirectory()) {
                List<String> tempFiles = getFiles(tempList[i].getPath());
                files.addAll(tempFiles);
            }
        }
        return files;
    }


    public static void outputToFile (String str, String path) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }

        OutputStream outputStream = new FileOutputStream(file);
        outputStream.write(str.getBytes());
        outputStream.close();
    }

    public static String getOutputFileName(String inputFileName) {
        String outputFileName = null;
        if(inputFileName.contains("\\")) {
            outputFileName = inputFileName.substring(inputFileName.lastIndexOf("\\") + 1);
            outputFileName = outputFileName.substring(0, outputFileName.indexOf("."));
            outputFileName = outputFileName.concat(".c");
        }
        else {
            outputFileName = inputFileName.substring(0, inputFileName.lastIndexOf("p") - 1);
        }

        return outputFileName;
    }

}
